#include "companyqrcodedialog.h"
#include "ui_companyqrcodedialog.h"

#include <KLocalizedString>
#include <KSharedConfig>
#include <KColorScheme>

CompanyQRCodeDialog::CompanyQRCodeDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CompanyQRCodeDialog)
{
    ui->setupUi(this);

    // 去除窗口图标、关于按钮和最小化按钮
    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
}

CompanyQRCodeDialog::CompanyQRCodeDialog(int num, QWidget *parent):
    CompanyQRCodeDialog(parent)
{
    // 设置窗口标题
    num ==1 ? setWindowTitle(i18n("Enterprise officialaccount")) : setWindowTitle(i18n("Product serviceofficialaccount"));
    ui->label->setStyleSheet(num == 1 ? QString("image:url(:/icons/qrcode_enterprise.png);") : QString("image:url(:/icons/qrcode_product.png);"));
}

CompanyQRCodeDialog::~CompanyQRCodeDialog()
{
    delete ui;
}

void CompanyQRCodeDialog::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);

    auto config = KSharedConfig::openConfig();
    auto highlitgtscheme = KColorScheme(QPalette::Active, KColorScheme::Selection, config);
    auto active = KColorScheme(QPalette::Active, KColorScheme::View, config);
    QColor thisbackground = active.background().color();
    QColor highlightcolor = highlitgtscheme.background(KColorScheme::NormalBackground).color();
    QColor textcolor = active.foreground(KColorScheme::NormalText).color();
     QString themebackground = QString("rgb(%1, %2, %3)").arg(thisbackground.red()).arg(thisbackground.green()).arg(thisbackground.blue());
    bool isLight = themebackground=="rgb(255, 255, 255)";

    if (isLight) {
        // 绘制标题栏背景，颜色设置为 #FFFFFF
        painter.fillRect(QRect(0, 0, width(), 30), QColor(255, 255, 255));

        // 绘制标题栏与主题之间的分隔线，颜色设置为 #CCCCCC
        painter.setPen(QColor(204, 204, 204)); // #CCCCCC
        painter.drawLine(0, 30, width(), 30);

        // 绘制标题
        painter.setPen(Qt::black); // 黑色文字
        painter.drawText(QRect(10, 0, width() - 50, 30), Qt::AlignLeft | Qt::AlignVCenter, windowTitle());

        // 绘制关闭按钮
        m_closeButtonRect = QRect(width() - 30, 5, 20, 20); // 调整关闭按钮的位置和大小
        painter.setRenderHint(QPainter::Antialiasing, true); // 开启抗锯齿

        // 绘制“×”符号
        painter.setPen(QPen(Qt::black, 2)); // 设置线条颜色和宽度
        painter.drawLine(m_closeButtonRect.topLeft() + QPoint(5, 5), m_closeButtonRect.bottomRight() - QPoint(5, 5)); // 左上到右下的线
        painter.drawLine(m_closeButtonRect.topRight() - QPoint(5, -5), m_closeButtonRect.bottomLeft() + QPoint(5, -5)); // 右上到左下的线
    } else {
        // 绘制标题栏背景，颜色设置为 #2D2D2D
        painter.fillRect(QRect(0, 0, width(), 30), QColor(45, 45, 45));

        // 绘制标题栏与主题之间的分隔线，颜色设置为 #CCCCCC
        painter.setPen(QColor(204, 204, 204)); // #CCCCCC
        painter.drawLine(0, 30, width(), 30);

        // 绘制标题
        painter.setPen(Qt::white); // 白色文字
        painter.drawText(QRect(10, 0, width() - 50, 30), Qt::AlignLeft | Qt::AlignVCenter, windowTitle());

        // 绘制关闭按钮
        m_closeButtonRect = QRect(width() - 30, 5, 20, 20); // 调整关闭按钮的位置和大小
        painter.setRenderHint(QPainter::Antialiasing, true); // 开启抗锯齿

        // 绘制“×”符号
        painter.setPen(QPen(Qt::white, 2)); // 设置线条颜色和宽度
        painter.drawLine(m_closeButtonRect.topLeft() + QPoint(5, 5), m_closeButtonRect.bottomRight() - QPoint(5, 5)); // 左上到右下的线
        painter.drawLine(m_closeButtonRect.topRight() - QPoint(5, -5), m_closeButtonRect.bottomLeft() + QPoint(5, -5)); // 右上到左下的线
    }

//    if(isLight)
//    {
//        // 绘制标题栏背景，颜色设置为 #FFFFFF
//        painter.fillRect(QRect(0, 0, width(), 30), QColor(255, 255, 255));

//        // 绘制标题栏与主题之间的分隔线，颜色设置为 #CCCCCC
//        painter.setPen(QColor(204, 204, 204)); // #CCCCCC
//        painter.drawLine(0, 30, width(), 30);

//        // 绘制标题
//        painter.setPen(Qt::black);
//        painter.drawText(QRect(10, 0, width() - 50, 30), Qt::AlignLeft | Qt::AlignVCenter, windowTitle());

//        // 绘制关闭按钮
//        m_closeButtonRect = QRect(width() - 30, 5, 20, 20); // 调整关闭按钮的位置和大小
//        painter.setRenderHint(QPainter::Antialiasing, true); // 开启抗锯齿

//        // 绘制圆形背景
//        painter.setBrush(QColor(240, 240, 240)); // 背景颜色
//        painter.setPen(Qt::NoPen); // 无边框
//        painter.drawEllipse(m_closeButtonRect);

//        painter.setPen(QPen(Qt::black, 2)); // 设置线条颜色和宽度
//        painter.drawLine(m_closeButtonRect.topLeft() + QPoint(5, 5), m_closeButtonRect.bottomRight() - QPoint(5, 5)); // 左上到右下的线
//        painter.drawLine(m_closeButtonRect.topRight() - QPoint(5, -5), m_closeButtonRect.bottomLeft() + QPoint(5, -5)); // 右上到左下的线
//    }
//    else
//    {
//        // 绘制标题栏背景，颜色设置为
//        painter.fillRect(QRect(0, 0, width(), 30), QColor(45, 45, 45));

//        // 绘制标题栏与主题之间的分隔线，颜色设置为 #CCCCCC
//        painter.setPen(QColor(255, 255, 255)); // #CCCCCC
//        painter.drawLine(0, 30, width(), 30);

//        // 绘制标题
//        painter.setPen(Qt::black);
//        painter.drawText(QRect(10, 0, width() - 50, 30), Qt::AlignLeft | Qt::AlignVCenter, windowTitle());

//        // 绘制关闭按钮
//        m_closeButtonRect = QRect(width() - 30, 5, 20, 20); // 调整关闭按钮的位置和大小
//        painter.setRenderHint(QPainter::Antialiasing, true); // 开启抗锯齿

//        // 绘制圆形背景
//        painter.setBrush(QColor(240, 240, 240)); // 背景颜色
//        painter.setPen(Qt::NoPen); // 无边框
//        painter.drawEllipse(m_closeButtonRect);

//        painter.setPen(QPen(Qt::white, 2)); // 设置线条颜色和宽度
//        painter.drawLine(m_closeButtonRect.topLeft() + QPoint(5, 5), m_closeButtonRect.bottomRight() - QPoint(5, 5)); // 左上到右下的线
//        painter.drawLine(m_closeButtonRect.topRight() - QPoint(5, -5), m_closeButtonRect.bottomLeft() + QPoint(5, -5)); // 右上到左下的线
//    }
}

void CompanyQRCodeDialog::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        if (m_closeButtonRect.contains(event->pos())) {
            close();
        } else {
            m_dragPosition = event->globalPos() - frameGeometry().topLeft();
            event->accept();
        }
    }
}

void CompanyQRCodeDialog::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton) {
        move(event->globalPos() - m_dragPosition);
        event->accept();
    }
}
